﻿using System.Text;
using ClosedXML.Excel;
using ExcelDataReader;

namespace ExcelLanguageAync
{
    internal class LanguageAync
    {
        private string mExcelsPath = @"Excels";
        private string mLanguageConfigPath = @"Excels/LanguageConfig.xlsx";

        public void Aync()
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            if (File.Exists(mLanguageConfigPath))
            {
                string[] files = Directory.GetFiles(mExcelsPath);
                foreach (string path in files)
                {
                    if (path.Contains("LanguageConfig_"))
                    {
                        List<string[]> languageConfigList = GetLanguageConfigList();
                        Dictionary<string, string> multiConfigDictionary = GetMultiLanguageConfigDictionary($@"{path}");

                        AyncConfigData(languageConfigList, multiConfigDictionary, $@"{path}");
                    }
                }

                Console.WriteLine("LanguageConfig同步完成");
            }
            else
            {
                Console.WriteLine("LanguageConfig.xlsx文件不存在");
            }
        }

        private List<string[]> GetLanguageConfigList()
        {
            List<string[]> dataList = new List<string[]>();

            using (FileStream stream = File.OpenRead(mLanguageConfigPath))
            {
                IExcelDataReader excelReader = ExcelReaderFactory.CreateReader(stream);
                while (excelReader.Read())
                {
                    string value1 = excelReader.GetString(0);
                    string value2 = excelReader.GetString(1);
                    dataList.Add(new string[] { value1, value2 });
                }
            }

            return dataList;
        }

        private Dictionary<string, string> GetMultiLanguageConfigDictionary(string pPath)
        {
            Dictionary<string, string> dataDictionary = new Dictionary<string, string>();

            using (FileStream fileStream = File.OpenRead(pPath))
            {
                IExcelDataReader excelReader = ExcelReaderFactory.CreateReader(fileStream);

                while (excelReader.Read())
                {
                    string value1 = excelReader.GetString(0);
                    string value2 = excelReader.GetString(1);
                    if (!dataDictionary.ContainsKey(value1))
                        dataDictionary.Add(value1, value2);
                    else
                        dataDictionary[value1] = "";
                }
            }

            return dataDictionary;
        }

        private void AyncConfigData(List<string[]> pLanguageConfigList, Dictionary<string, string> pMultiConfigDictionary, string pPath)
        {
            XLWorkbook targetWorkbook = new XLWorkbook();
            IXLWorksheet targetWorksheet = targetWorkbook.Worksheets.Add("Sheet1");

            for (int i = 0; i < pLanguageConfigList.Count; i++)
            {
                string id = pLanguageConfigList[i][0];
                targetWorksheet.Cell(i + 1, 1).Value = id;
                if (pMultiConfigDictionary.ContainsKey(id))
                    targetWorksheet.Cell(i + 1, 2).Value = pMultiConfigDictionary[id];
                else
                    targetWorksheet.Cell(i + 1, 2).Value = pLanguageConfigList[i][1];
            }

            targetWorkbook.SaveAs(pPath);
            Console.WriteLine(pPath);
        }
    }
}
